﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CITWebsite.Models;
using DTO.Models;

namespace CITWebsite.Controllers
{
    public class SurveyController : Controller
    {
        //
        // GET: /Survey/

        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult SurveyList()
        {
            return View();
        }

        [Authorize]
        [HttpGet]
        public ActionResult List(string id, string pDateFrom, string pDateTo, string pIsTop)
        {
            //id : pTypeMediaName
            int PageSize = Common.pagesize;
            int PageIndex = Convert.ToInt32(id);
            
            bool IsTop = pIsTop == null ? false : (pIsTop == "true" ? true : false);
            DateTime DateForm = pDateFrom == null ? Common.datemin : Common.ChangeDateVNToUS(pDateFrom);
            DateTime DateTo = pDateTo == null ? Common.datemax : Common.ChangeDateVNToUS(pDateTo);

            DataRepository dr = new DataRepository();
            
            SURVEY[] source;
            if (IsTop == true)
                source = dr.SurveyProvider.getSurvey(DateForm, DateTo, IsTop).ToArray();
            else
                source = dr.SurveyProvider.getSurvey(DateForm, DateTo).ToArray();

            SURVEY[] datapaging = source.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToArray();

            int len = source.Length;

            ViewData["TotalPage"] = (len % PageSize == 0 ? len / PageSize : len / PageSize + 1).ToString();
            ViewData["PageIndex"] = PageIndex.ToString();
            ViewData["PageSize"] = PageSize.ToString();
            return View(datapaging);

        }

        [Authorize]
        public ActionResult Delete(string id)
        {
            DataRepository dr = new DataRepository();
            dr.SurveyProvider.DeleteMark(id);
            return Content("");
        }

        [Authorize]
        public ActionResult GetForm(string id)
        {
            SURVEY obj = null;
            //add new
            if (id == "0")
            {
                obj = new SURVEY(0);
            }
            else
            {
                DataRepository dr = new DataRepository();
                obj = dr.SurveyProvider.getSurveyById(Convert.ToInt32(id));
            }
            return View(obj);
        }

        [Authorize]
        public ActionResult Active(string id)
        {
            //add new
            if (id != null)
            {
                DataRepository dr = new DataRepository();
                dr.SurveyProvider.Active(id);
            }
            return RedirectToAction("SurveyList");
        }

        [Authorize]
        [HttpPost]
        public ActionResult GetForm(SURVEY obj)
        {
            DataRepository dr = null;

            if (!ModelState.IsValid)
                return View(obj);
            else
            {
                //insert
                if (obj.ID == 0)
                {
                    dr = new DataRepository();
                    dr.SurveyProvider.Insert(obj);
                }
                else
                {
                    dr = new DataRepository();
                    dr.SurveyProvider.Update(obj);
                }

                return RedirectToAction("SurveyList");
            }

        }

    }
}
